<?php

/** 
 * 离线订单缓存队列
 */
class Order_offline_sync_queue_mdl extends Mongo_Model {
	
	public function __construct() {
		$this->collName = 'orderOfflineSyncQueue';
		parent::__construct();
	}
	
	//入队列
	public function enqueue($order,$userId){
		$time = time();
		$doc = array(
				'odetail'=>$order,//订单详情
				'uid'=>(int)$userId,
				'state'=>0,//用户ID
				'ctime'=>$time,// 主表订单ID
				'utime'=>0,
		);
		return $this->insert($doc);
	}
	
	//出队列一次处理50条
	public function dequeue(){
		$where = array('state'=>0);
		$list = $this->coll->find($where)->limit(50);
		$res = $this->toArray($list);
		return $res;
	}
	
	//更新正在处理
	public function up($id){
		if (empty($id)){
			return false;
		}
		$time = time();
		$_id = new \MongoId($id);
		$update = array('$set'=>array('state'=>1,'utime'=>$time));
		$where = array('_id'=>$_id);
		$res = $this->coll->update ( $where, $update );
		return !empty($res['ok']);
		
	}
	
	//清理队列
	public function clean($id){
		if ( empty($id) ){
			return false;
		}
		$_id = new \MongoId($id);
		$where = array('_id'=>$_id);
		return $this->coll->remove($where);
	}
	
}

